package com.lihuinch.test.feishu.eventsubscribe.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lihuinch.test.feishu.eventsubscribe.constant.FeishuConstant;
import com.lihuinch.test.feishu.eventsubscribe.domain.Allocations;
import com.lihuinch.test.feishu.eventsubscribe.domain.ReimburseData;
import com.lihuinch.test.feishu.eventsubscribe.domain.Vouchers;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author：岳彩郑
 * @desc：飞书凭证接口
 * @date：2022/10/9 9:52
 **/

@Service
public class FeishuVouchersApi {
    /**
     * 飞书凭证接口 异步调用
     *
     * @param param get类型的接口，传值为官方接口为准
     * @return
     */
    @Async
    public void FeishuVouchersApi(String param) {
        System.out.println("Execute method asynchronously - "
                + Thread.currentThread().getName());
        try {
            Thread.sleep(2000);
            //获取Token
            GetFeishuToken getFeishuToken = new GetFeishuToken();
            String token = getFeishuToken.getToken();
            //调用凭证接口
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Bearer " + token);
            HttpEntity<String> entity = new HttpEntity<>(headers);
            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<JSONObject> response = null;
            response = restTemplate.exchange(FeishuConstant.GET_Vouchers_URL + "?" + param, HttpMethod.GET, entity, JSONObject.class);

            if (StringUtils.isEmpty(response.getBody())) {
                return;
            }

            //调用ERP接口的头信息
//            ERPApiGetHeadContext erpApiGetHeadContext = new ERPApiGetHeadContext();
//            JSONObject requestParams = erpApiGetHeadContext.getContext();

            //返回的数据处理
            JSONObject body = response.getBody();
            JSONObject data = body.getJSONObject("data");
            JSONArray items = data.getJSONArray("items");
            List<Vouchers> vouchers = JSONObject.parseObject(items.toJSONString(), new TypeReference<List<Vouchers>>() {
            });
            vouchers.forEach(item1 -> {
                List<ReimburseData> reimburse_data = item1.getReimburse_data();
                for (ReimburseData reimburse_datum : reimburse_data) {
                    List<Allocations> allocations = reimburse_datum.getAllocations();
                    allocations.forEach(item2 -> {
                        //requestParams.put("name","sss");
                        System.out.println(item2.getReimburse_line_id());
                    });
                }
            });


            //todo 调用ERP接口

            //todo  在这里对body进行处理

        } catch (InterruptedException e) {
            //
        }
    }

}
